package com.cs.widgetvp.activity;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.MenuItem;

import androidx.annotation.NonNull;

import com.cs.base.BaseActivity;
import com.cs.base.BaseFragment;
import com.cs.base.adapter.BaseFragmentPagerAdapter;
import com.cs.base.util.StatusBarUtil;
import com.cs.base.vm.BaseViewModel;
import com.cs.widgetvp.R;
import com.cs.widgetvp.databinding.ActivityMainVpBinding;
import com.cs.widgetvp.fragment.CustomerFragment;
import com.cs.widgetvp.fragment.GetherFragment;
import com.cs.widgetvp.fragment.HomeFragment;
import com.cs.widgetvp.fragment.MyFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivityVP extends BaseActivity<BaseViewModel, ActivityMainVpBinding> {

    private final List<BaseFragment> fragments = new ArrayList<>();
    @Override
    protected ActivityMainVpBinding getViewBinding() {
        return ActivityMainVpBinding.inflate(LayoutInflater.from(this));
    }

    @Override
    protected void initView() {
        StatusBarUtil.setTransparent(MainActivityVP.this);
        initViewPage();
        initPageIndex();
    }

    @Override
    protected void initData() {

    }

    /**
     * 初始化ViewPage
     */
    private void initViewPage(){
        if(fragments.size()>0){
            return;
        }
        fragments.add(new HomeFragment());
        fragments.add(new GetherFragment());
        fragments.add(new CustomerFragment());
        fragments.add(new MyFragment());
        binding.mainVp.setCanScroll(false);
        BaseFragmentPagerAdapter adapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(),fragments);
        binding.mainVp.setAdapter(adapter);
        binding.mainVp.setOffscreenPageLimit(4);
    }

    private void initPageIndex(){
        binding.mainBottomBar.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);//设置Label一直显示
        binding.mainBottomBar.setItemIconTintList(null);//禁用投影原生选择效果，否则图片只剩投影
        binding.mainBottomBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int itemId = item.getItemId();
                if (itemId == R.id.main_nv_home) {
                    StatusBarUtil.setTransparent(MainActivityVP.this);
                    binding.mainVp.setCurrentItem(0);
                    return true;
                } else if (itemId == R.id.main_nv_gether) {
                    StatusBarUtil.setColor(MainActivityVP.this, "#009AB6");
                    binding.mainVp.setCurrentItem(1);
                    return true;
                } else if (itemId == R.id.mian_nv_customer) {
                    StatusBarUtil.setColor(MainActivityVP.this, "#009AB6");
                    binding.mainVp.setCurrentItem(2);
                    return true;
                } else if (itemId == R.id.mian_nv_my) {
                    StatusBarUtil.setColor(MainActivityVP.this, "#009AB6");
                    binding.mainVp.setCurrentItem(3);
                    return true;
                }
                return false;
            }
        });
    }


}